package com.decent.ejfadmin.user.dao.provider;

import com.decent.ejfadmin.common.bean.SupCertificateDto;
import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.user.bean.Const;
import com.decent.ejfadmin.user.bean.SupCertificateRecordDto;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Objects;

/**
 * describe:会员实名认证管理
 *
 * @author JunLei He
 * @date 2018年10月13日
 */
public class UserCertificateProvider {
    /**
     * 查询会员实名认证信息
     *
     * @param map 参数集合
     * @return 返回sql
     */
    public String queryUserCertificateList(HashMap<String, Object> map) {
        SupUser supUser = (SupUser) map.get("supUser");
        SupCertificateDto supCertificateDto = (SupCertificateDto) map.get("supCertificateDto");
        StringBuffer sql = new StringBuffer(100);
        sql.append(" SELECT " +
                " c.id, " +
                " c.user_id userId, " +
                " c.alipay, " +
                " c.cert_level certLevel, " +
                " c.citizen_id_num citizenIdNum, " +
                " c.citizen_id_salt citizenIdSalt, " +
                " c.e_signature eSignature, " +
                " c.front_pic_url frontPicUrl, " +
                " DATE_FORMAT(c.gmt_create,'%Y-%m-%d %H:%i:%s') startTime, " +
                " DATE_FORMAT(c.gmt_modified,'%Y-%m-%d %H:%i:%s') endTime, " +
                " c.handing_pic_url handingPicUrl, " +
                " c.level_state levelState, " +
                " c. NAME, " +
                " c.reverse_pic_url reversePicUrl, " +
                " c.success_level successLevel, " +
                " c.remark, " +
                " i.name nickName, " +
                " c.contract_id contractId, " +
                " u.account " +
                "FROM " +
                " yuhuang_sup_certificate c, " +
                " yuhuang_sup_user U, yuhuang_sup_info i " +
                "WHERE " +
                " u.user_id = c.user_id AND i.user_id=u.user_id ");
        if (StringUtils.isNotBlank(supUser.getAccount())) {
            sql.append(" AND (u.account = #{supCertificateDto.account} or instr(i.name,#{supCertificateDto.account})>0) ");
        }
        if (StringUtils.isNotBlank(supCertificateDto.getStartTime())) {
            sql.append(" and c.gmt_create >= #{supCertificateDto.startTime} ");
        }
        if (StringUtils.isNotBlank(supCertificateDto.getEndTime())) {
            sql.append(" and c.gmt_create <= #{supCertificateDto.endTime} ");
        }
        if (Objects.nonNull(supCertificateDto.getCertLevel())) {
            sql.append(" AND c.cert_level= #{supCertificateDto.certLevel} ");
        }

        if (Objects.nonNull(supCertificateDto.getSuccessLevel())) {
            sql.append(" AND c.success_level= #{supCertificateDto.successLevel} ");
        }

        if (Objects.nonNull(supCertificateDto.getLevelState())) {
            sql.append(" AND c.level_state= #{supCertificateDto.levelState} ");
        }
        if(StringUtils.isNotBlank(supCertificateDto.getName())){
            sql.append(" AND c.name= #{supCertificateDto.name} ");
        }

        sql.append(" ORDER BY c.gmt_create desc ");

        return sql.toString();
    }

    /**
     * 修改会员实名认证状态
     *
     * @param param 参数集合
     * @return 返回sql语句
     */
    public String updateUserCertificate(HashMap<String, Object> param) {
        String remark = (String) param.get("remark");
        Integer status = (Integer) param.get("status");
        StringBuffer sql = new StringBuffer(60);
        sql.append(" UPDATE yuhuang_sup_certificate SET gmt_modified  = NOW(), level_state  = #{status} ");

        if (StringUtils.isNotBlank(remark)) {
            sql.append(" , remark  = CONCAT(remark,#{remark}) ");
        }

        //认证成功
        if (Const.SUCCESS_CERT.equals(status)) {
            sql.append(" ,success_level = cert_level ");
        }

        sql.append(" WHERE  id  = #{id} and success_level < cert_level ");

        return sql.toString();
    }

    /**
     * 创建实名认证记录
     *
     * @param param 参数集合
     * @return sql
     */
    public String createSupCertificateRecord(HashMap<String, SupCertificateRecordDto> param) {
        SupCertificateRecordDto supCertificateRecordDto = param.get("supCertificateRecordDto");
        StringBuilder sql = new StringBuilder(50);
        StringBuilder valueSql = new StringBuilder(50);
        sql.append(" INSERT INTO `yuhuang_sup_certificate_record` ( `user_id`, `cert_level`, `level_state` ");
        valueSql.append(" ) VALUES ( #{supCertificateRecordDto.userId},#{supCertificateRecordDto.certLevel},#{supCertificateRecordDto.levelState}");

        if (StringUtils.isNotBlank(supCertificateRecordDto.getBizNo())) {
            sql.append(" ,`biz_no` ");
            valueSql.append(" ,#{supCertificateRecordDto.bizNo} ");
        }

        sql.append(valueSql.toString());
        sql.append(" ) ");
        return sql.toString();
    }
}
